package LeetCodeP;
/*删除链表中等于给定值 val 的所有节点。
        示例:
        输入: 1->2->6->3->4->5->6, val = 6
        输出: 1->2->3->4->5*/
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution203 {


    public  ListNode removeElements5(ListNode head,int val){

        if (head==null){
            return null;
        }

        head.next=removeElements5(head.next,val);
        return head.val==val?head.next:head;

    }

    /**
     * 递归思想求解
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements4(ListNode head,int val){

        if (head==null){
            return null;
        }
        ListNode res=removeElements4(head.next,val);
        if(head.val==val){
            return res;
        }else {
            head.next=res;
            return head;
        }

    }



    public ListNode removeElements3(ListNode head,int val){

        //创建虚拟结点
        ListNode dummyHead=new ListNode(-1);
        dummyHead.next=head;

        ListNode prev=dummyHead;
        while (prev.next!=null){
            if (prev.next.val==val){
                prev.next=prev.next.next;
            }else {
                prev=prev.next;
            }

        }

        return dummyHead.next;
    }

        public ListNode removeElements2(ListNode head,int val){

        while (head!=null&&head.val==val){
            ListNode delNode=head;
            head=head.next;
            delNode.next=null;
        }

        if (head==null){
            return null;
        }

        ListNode prev=head;
        while (prev.next!=null){
            if (prev.next.val==val){
                prev.next=prev.next.next;

            }else {
                prev=prev.next;
            }
        }

        return head;


    }


    public ListNode removeElements1(ListNode head,int val){

        //结点不为空，并且结点刚好等于val
        while (head!=null&&head.val==val){
            ListNode delNode=head;
            head=head.next;
            delNode.next=null;
        }
        //结点为空
        if (head==null){
            return null;
        }
        ListNode prev=head;
        while (prev.next!=null){
            if(prev.next.val==val){
                ListNode delNode=prev.next;
                prev.next=delNode.next;
                delNode.next=null;

            }else {
                prev=prev.next;
            }
        }
        return head;

        }

    public static void main(String[] args) {

        int[] nums={1,2,6,3,4,5,6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res1=(new Solution203().removeElements1(head,6));
        System.out.println(res1);
        ListNode res2=(new Solution203().removeElements2(head,6));
        System.out.println(res2);
        ListNode res3=(new Solution203().removeElements3(head,6));
        System.out.println(res3);
        ListNode res4=(new Solution203().removeElements4(head,6));
        System.out.println(res4);
        ListNode res5=(new Solution203().removeElements5(head,6));
        System.out.println(res5);

    }

}
